<?php
class Wmp_View_Helper_WmpLoginPanel extends Zend_View
{
	public function WmpLoginPanel()
    {
    	//OPTIMIZEME:trouver un meilleur moyen de recuperer l'object view
		$viewRenderer =  Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
		$view = $viewRenderer->view;
		        
		$panel="";
    	$auth = Zend_Auth::getInstance();
    	$userdata = new Zend_Session_Namespace('userdata');
        if ($auth->hasIdentity()) {
        	$user = $auth->getIdentity();
        	
        	$userRoles="";
        	$listRoles = explode(';',$user->role);
        	if(count($listRoles)>1){
        		$userRoles .= '<div>';
        		$userRoles .= 'Mon profil : '.$userdata->userrole; 
        		$userRoles .= '<FORM method="post">';
        		$userRoles .= '<SELECT name="userrole" onChange="submit()">';
        		$userRoles .= '<OPTION value="">Changer de profil</OPTION>';
        		foreach($listRoles as $role)
        			$userRoles .= '<OPTION value="'.$role.'">'.$role.'</option>';
        		$userRoles .= '</SELECT>';
        		$userRoles .= '</FORM></div>';
        	}
        	
        	
            $url = $view->url(array('module'=>'member','controller'=>'profile','action'=>'info','view'=>$user->username));
            $logoutUrl = $view->url(array('module'=>'member','controller'=>'login','action'=>'logout'));
            $panel ='<div id="loginPanel">';
            $panel .= '<a href="'.$url.'">Welcome, ' . $user->firstname.' '.$user->lastname  
            															.' ('.$user->username .  ')</a>';
            $panel .= $userRoles;			            				
            $panel .= '<div id="logout"><a href="'.$logoutUrl.'">'.$view->WmpImg('logout-icon.jpg','Wmp Framework',false,'border="0" valign="top"').'</a></div>';
            $panel .= '</div>';
        }
        else{		
	        //OPTIMIZEME:trouver un moyen d'optimiser ce bout de code
	        //pour la recuperation des noms de module, controlleur, action
	        $fc = Zend_Controller_Front::getInstance();
			$request	=$fc->getRequest();
			$module		= $request->getModuleName();
	        $controller	= $request->getControllerName();
	        $action		= $request->getActionName();
	        if(!($module=='member' && $controller=="login" && $action=="index")){ 
	        	$loginUrl = $view->url(array('module'=>'member','controller'=>'login','action'=>'index'));
	        	$panel ='<div id="loginPanel"><a href="'.$loginUrl.'">Login</a></div>';	        	
	        }
        }
        
       return $panel;
    }
}
